package gui.obu;

import gui.obu.about.ObuAboutPanel;
import gui.obu.about.ObuAboutPanelImpl;
import gui.obu.about.ObuDriverInfoPanel;
import gui.obu.about.ObuDriverInfoPanelImpl;
import gui.obu.about.ObuOwnerInfoPanel;
import gui.obu.about.ObuOwnerInfoPanelImpl;
import gui.obu.alerts.AbstractAlert;
import gui.obu.alerts.DistanceAlert;
import gui.obu.alerts.SeatBeltAlert;
import gui.obu.alerts.SpeedAlert;
import gui.obu.alerts.StopSignAlert;
import gui.obu.alerts.panels.AbstractAlertPanel;
import gui.obu.alerts.panels.ObuFollowingAlertPanelImpl;
import gui.obu.alerts.panels.ObuSpeedLimitAlertPanelImpl;
import gui.obu.alerts.panels.ObuStopSignAlertPanelImpl;

import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.Queue;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.swtdesigner.SWTResourceManager;

public class ObuDrivingDisplayPanelImpl extends AbstractObuPanelImpl implements ObuDrivingDisplayPanel {
	
	private Label followingDistanceLbl;
	private Label speedLimitLbl;
	private Label currentSpeedLbl;
	private Button background;
	private Label mph;
	private Label followingDist;
	private Label speedLimit;
	private Label currentSpeed;
	private Button button_1;
	private Label mph2;
	private Label mph3;
	private Button about;
	private Button ownerInfo;
	private Button driverInfo;
	private Queue<AbstractAlert> alerts = new LinkedList<AbstractAlert>();
	
	public ObuDrivingDisplayPanelImpl(Display display, Shell shell) {
		super(display, shell);
		
		ObuSidePanel side = new ObuSidePanelImpl(display, new Shell());
		side.open();
		side.setWindow(this);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	public void open() {
		// remove at compile time
//		Shell shell = new Shell();
		// **********************
		
		shell.setSize(450, 300);
		shell.setText("Driving Display");
		
		followingDistanceLbl = new Label(shell, SWT.NONE);
		followingDistanceLbl.setBounds(32, 57, 125, 15);
		followingDistanceLbl.setText("Following Distance:");
		followingDistanceLbl.setFont(new Font(display, "Arial", 10, SWT.BOLD));
		followingDistanceLbl.setBackground(SWTResourceManager.getColor(240, 240, 240));
		
		speedLimitLbl = new Label(shell, SWT.NONE);
		speedLimitLbl.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		speedLimitLbl.setBackground(SWTResourceManager.getColor(200, 200, 200));
		speedLimitLbl.setBounds(268, 57, 81, 15);
		speedLimitLbl.setText("Speed Limit:");
		speedLimitLbl.setFont(new Font(display, "Arial", 10, SWT.BOLD));
		
		currentSpeedLbl = new Label(shell, SWT.NONE);
		currentSpeedLbl.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
		currentSpeedLbl.setBackground(SWTResourceManager.getColor(200, 200, 200));
		currentSpeedLbl.setBounds(268, 102, 96, 15);
		currentSpeedLbl.setText("Current Speed:");
		currentSpeedLbl.setFont(new Font(display, "Arial", 10, SWT.BOLD));
		
		mph = new Label(shell, SWT.NONE);
		mph.setBounds(102, 78, 25, 15);
		mph.setText("ft");
		
		followingDist = new Label(shell, SWT.NONE);
		followingDist.setBounds(42, 78, 55, 15);
		followingDist.setFont(new Font(display, "Arial", 9, SWT.BOLD));
		
		speedLimit = new Label(shell, SWT.NONE);
		speedLimit.setBounds(268, 78, 81, 18);
		speedLimit.setFont(new Font(display, "Arial", 9, SWT.BOLD));
		
		currentSpeed = new Label(shell, SWT.NONE);
		currentSpeed.setBounds(268, 123, 81, 18);
		currentSpeed.setFont(SWTResourceManager.getFont("Arial", 9, SWT.BOLD));
		
		button_1 = new Button(shell, SWT.NONE);
		button_1.setImage(SWTResourceManager.getImage(ObuDrivingDisplayPanelImpl.class, "/images/seatbeltGreen.gif"));
		button_1.setBounds(166, 175, 100, 79);
		
		mph2 = new Label(shell, SWT.NONE);
		mph2.setBounds(355, 81, 25, 15);
		mph2.setText("mph");
		mph2.setBackground(SWTResourceManager.getColor(200, 200, 200));
		
		mph3 = new Label(shell, SWT.NONE);
		mph3.setBounds(355, 126, 25, 15);
		mph3.setText("mph");
		mph3.setBackground(SWTResourceManager.getColor(200, 200, 200));
		
		about = new Button(shell, SWT.NONE);
		about.setBounds(10, 229, 75, 25);
		about.setText("About");
		about.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				ObuAboutPanel window = new ObuAboutPanelImpl(display, shell);
				hide(true);
				window.open();
			}
		});
		
		ownerInfo = new Button(shell, SWT.NONE);
		ownerInfo.setBounds(10, 202, 75, 25);
		ownerInfo.setText("Owner");
		ownerInfo.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				ObuOwnerInfoPanel window = new ObuOwnerInfoPanelImpl(display, shell);
				hide(true);
				window.open();
			}
		});
		
		driverInfo = new Button(shell, SWT.NONE);
		driverInfo.setBounds(349, 202, 75, 25);
		driverInfo.setText("Driver");
		driverInfo.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				ObuDriverInfoPanel window = new ObuDriverInfoPanelImpl(display, shell);
				hide(true);
				window.open();
			}
		});
		
		background = new Button(shell, SWT.NONE);
		background.setImage(SWTResourceManager.getImage(ObuDrivingDisplayPanelImpl.class, "/images/driverDisplayBkgrnd.gif"));
		background.setBounds(0, 0, 434, 264);
		background.setEnabled(false);

		shell.open();
		shell.layout();
	}
	
	@Override
	public void setSpeedLimit(Object text) {
		this.speedLimit.setText((String)text);
		this.speedLimit.update();
	}
	
	@Override
	public void setCurrentSpeed(Object text) {
		this.currentSpeed.setText((String)text);
		this.speedLimit.update();
	}
	
	@Override
	public void setFollowingDistance(Object text) {
		DecimalFormat df = new DecimalFormat("#.##");
		this.followingDist.setText(df.format((Double) text * 3.28));
		this.followingDist.update();
	}
	
	@Override
	public String getSpeedLimit() {
		return this.speedLimit.getText();
	}
	
	@Override
	public String getCurrentSpeed() {
		return this.currentSpeed.getText();
	}
	
	@Override
	public String getFollowingDistance() {
		return this.followingDist.getText();
	}
	
	public void addAlert(AbstractAlert alert) {
		if(!alerts.add(alert)) {
			System.out.println("Unable to add alert " + alert);
		} else {
			issueAlert();
		}
	}
	
	public void issueAlert() {
		while(!alerts.isEmpty()) {
			// pop
			AbstractAlert alert = alerts.poll();
			AbstractAlertPanel panel;
			
			// check instanceof alert & create alert
			if(alert instanceof SpeedAlert) {
				panel = new ObuSpeedLimitAlertPanelImpl(this.display, new Shell());
				((ObuSpeedLimitAlertPanelImpl) panel).setSpeedLimit(((SpeedAlert) alert).getSpeedLimit());
				((ObuSpeedLimitAlertPanelImpl) panel).setCurrentSpeed(((SpeedAlert)alert).getCurrentSpeed());
				panel.open();
			} else if(alert instanceof DistanceAlert) {
				panel = new ObuFollowingAlertPanelImpl(this.display, new Shell());
				((ObuFollowingAlertPanelImpl)panel).setDistance(((DistanceAlert)alert).getCurrentDistance());
				((ObuFollowingAlertPanelImpl)panel).setSpeed(((DistanceAlert)alert).getCurrentSpeed());
				panel.open();
			} else if(alert instanceof SeatBeltAlert) {
				if(((SeatBeltAlert)alert).getEngaged()) {
					button_1.setImage(SWTResourceManager.getImage(ObuDrivingDisplayPanelImpl.class, "/images/seatbeltGreen.gif"));
				} else {
					button_1.setImage(SWTResourceManager.getImage(ObuDrivingDisplayPanelImpl.class, "/images/seatbeltRed.gif"));
				}
			} else if(alert instanceof StopSignAlert) {
				panel = new ObuStopSignAlertPanelImpl(this.display, new Shell());
				panel.open();
			} else {
				System.out.println("Found incompatible alert!!!");
			}
		}
	}
	
	public Display getDisplay() {
		return display;
	}
}
